package com.orivon.mob.hook.helper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.orivon.mob.hook.HookApplication;
import com.orivon.mob.hook.bean.SupportVer;

import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class DaoHandler {
    private static MyDatabaseHelpser helper;
    private static DaoHandler instance;
    private DaoHandler() {

    }



    public static DaoHandler getInstance(Context context) {
        if (instance == null) {
            instance = new DaoHandler();
            helper = new MyDatabaseHelpser(context);
        }
        return instance;
    }

    public Cursor query(String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteDatabase readableDatabase = helper.getReadableDatabase();
        return readableDatabase.query(MyDatabaseHelpser.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
    }

    public void add(ContentValues values) {
        SQLiteDatabase writableDatabase = helper.getWritableDatabase();
        writableDatabase.insert(MyDatabaseHelpser.TABLE_NAME, null, values);
    }


    public List<CharSequence> getNumList() {
        List<CharSequence> list = new ArrayList<>();
        SQLiteDatabase readableDatabase = helper.getReadableDatabase();
        Cursor cursor = readableDatabase.query(MyDatabaseHelpser.TABLE_NAME, new String[]{"dice_num"}, null, null, null, null, null);
        while (cursor != null && cursor.moveToNext()) {

            list.add(String.valueOf(cursor.getInt(0) + 1));
        }
        return list;
    }

    public int delectNum(int id) {
        SQLiteDatabase readableDatabase = helper.getReadableDatabase();
        return readableDatabase.delete(MyDatabaseHelpser.TABLE_NAME, "_id=?", new String[]{String.valueOf(id)});
    }


    public void saveSupVer(List<SupportVer> list) {
        SQLiteDatabase readableDatabase = helper.getReadableDatabase();
        for (SupportVer ver : list) {
            Cursor co = readableDatabase.query(MyDatabaseHelpser.TABLE_VER, null, "ver_name=?", new String[]{ver.getVerName()}, null, null, null);
            if (co == null || !co.moveToNext()) {
                ContentValues values = new ContentValues();
                values.put("ver_name", ver.getVerName());
                values.put("cla_name", ver.getcName());
                values.put("fun_name", ver.getfName());
                readableDatabase.insert(MyDatabaseHelpser.TABLE_VER, null, values);
            } else {
                System.out.println(ver.getVerName() + "已经存在了");
            }
        }
    }


    public Cursor getSupVer(String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteDatabase readableDatabase = helper.getReadableDatabase();
        return readableDatabase.query(MyDatabaseHelpser.TABLE_VER, projection, selection, selectionArgs, null, null, sortOrder);
    }

    public void clearData() {
        SQLiteDatabase readableDatabase = helper.getReadableDatabase();
        readableDatabase.delete(MyDatabaseHelpser.TABLE_NAME, null, null);
    }

}
